
package com.zktr.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.zktr.pojo.AdminRole;

public class XlsMain {
	public static void main(String[] args) throws IOException {
		XlsMain xlsMain = new XlsMain();
		List<AdminRole> readXls = xlsMain.readXls();
		System.out.println(readXls);
		AdminRoleDao adminRoleDao = new AdminRoleDao();
		for (AdminRole adminRole : readXls) {
			adminRoleDao.insertOneAdmin(adminRole);
		}
		System.out.println(adminRoleDao);
	/*	try {
			XlsDto2Excel.xlsDto2Excel(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (int i = 0; i < list.size(); i++) {
			xls = (AdminRole) list.get(i);
			System.out.println(
				);
		}*/

	}

	private List<AdminRole> readXls() throws IOException {
		InputStream is = new FileInputStream("e:\\admin.xls");
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
		
		AdminRole ar = null;
		List<AdminRole> list = new ArrayList<AdminRole>();
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			
			if (hssfSheet == null) {
				continue;
			}
			System.out.println(hssfSheet.getLastRowNum());
			// 循环行Row
			for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				if (hssfRow == null) {
					continue;
				}
				ar = new AdminRole();
				
				HSSFCell yxsmc = hssfRow.getCell(0);
				if (yxsmc == null) {
					continue;
				}
				
				ar.setAdminrole(Integer.parseInt(getValue(yxsmc)));
				HSSFCell xm = hssfRow.getCell(1);
			
				if (xm == null) {
					continue;
				}
				ar.setUserid(Integer.parseInt(getValue(xm)));
				HSSFCell xh = hssfRow.getCell(2);
				if (xh == null) {
					continue;
				}
				ar.setRoleid(Integer.parseInt(getValue(xh)));
	
			}
			list.add(ar);
		}
		return list;
	}

	/**
	 * 得到Excel表中的值
	 * 
	 * @param hssfCell
	 *            Excel中的每一个格子
	 * @return Excel中每一个格子中的值
	 */
	@SuppressWarnings("static-access")
	private String getValue(HSSFCell hssfCell) {
		if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
			// 返回布尔类型的值
			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
			// 返回数值类型的值
			return String.valueOf(hssfCell.getNumericCellValue());
		} else {
			// 返回字符串类型的值
			return String.valueOf(hssfCell.getStringCellValue());
		}
	}
}

